const http = require('http')
const url = require('url')
const fs = require('fs')

function getFile(filename) {
  return fs.readFileSync(filename)
}

const server = http.createServer((req, res) => {
  let { pathname } = url.parse(req.url)
  switch(pathname) {
    case '/':
      res.end(getFile('./public/index.html'))
      break
    case '/style.css':
      res.writeHead(200, {
        'content-type': 'text/css'
      })
      res.end(getFile('./public/style.css'))
      break
    case '/index.js':
      res.end(getFile('./public/index.js'))
      break
    default:
      res.end(getFile('./public/page404.html'))
      break
  }
})

server.listen(8080, () => {
  console.log('localhost:8080')
})